<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('user', '', true);
        $this->load->model("check_institute_id");
        $this->load->library('authentication');
        $this->load->helper('t');


        if ($this->session->userdata('logged_in')) {
            $this->LoggedUser = $this->user->getLoggedUser();
            redirect(get_folder($this->LoggedUser->title) . '/home');
        }

//        Authentication::redirectIfLogged();
    }

    function index($institute_id = 'MQ%3D%3D') {
//        if ($this->session->userdata('logged_in')) {
//            
//            redirect('home');
//        }

        Authentication::redirectIfLogged();

        if ($this->input->post('Command') == 'Login') {
            $returnData = $this->_PostLogin();
        }

        $institute_id = base64_decode(urldecode($institute_id));
        $result = $this->check_institute_id->check_ins_id($institute_id);
        if ($result == 0) {
            $this->load->view('invalid_url');
        } else {
            if ($result['status'] == 0) {
                redirect('login/status_deactivate');
            }
            $this->session->set_userdata('institute_id', $institute_id);
            $this->session->set_userdata('organization_name', $result['organization_name']);
            $this->session->set_userdata('contact_info', $result['contact_info']);
            $this->session->set_userdata('logo', $result['logo']);

            $viewData['logo'] = $result['logo'];
            $viewData['organization_name'] = $result['organization_name'];
            $viewData['contact_info'] = $result['contact_info'];

            $this->load->helper(array('form'));
            $viewData['title'] = "Login";

            foreach ($returnData as $key => $value) {
                $viewData[$key] = $value;
            }


            //$this->load->view('header', $viewData);
            $this->load->view('general/login', $viewData); //$this->load->view('login_view', $data);
            //$this->load->view('footer', $viewData);
        }
    }

    function status_deactivate() {
        $this->load->view('status_deactive');
    }

    function _PostLogin() {
        //This method will have the credentials validation
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        //$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_message('required', 'Chưa nhập %s');

        if ($this->form_validation->run() == FALSE) {
//            $returnData['organization_name'] = $this->session->userdata('organization_name');
//            $returnData['contact_info'] = $this->session->userdata('contact_info');
//            $returnData['logo'] = $this->session->userdata('logo');
            //Field validation failed.&nbsp; User redirected to login page
//            $returnData['title'] = "Login";
//            $this->load->view('header', $data);
//            $this->load->view('general/login', $data);//$this->load->view('login_view', $data);
//            $this->load->view('footer', $data);
//            return $returnData;

            return false;
        }
        $username = $this->input->post('username');
        $user = $this->user->get_user_by_username($username);

        $allowedTitles = array('SD', 'HR', 'SC', 'RSM', 'KAM', 'SS', 'KAS');

        if (in_array($user['title'], $allowedTitles) == true) {
            $returnData = Authentication::login($username, $this->input->post('password'));

            if ($returnData['Success'] == true) {
                redirect(get_folder($returnData['LoggedUser']->title) . '/home');
            } else {
                return $returnData;
            }
        } else {
            return array('Success' => false, 'Message' => 'Sai username hoặc password');
        }
    }

//    function check_database($password) {
//        //Field validation succeeded.&nbsp; Validate against database
//        $username = $this->input->post('username');
//        //query the database
//        $result = $this->user->login($username, $password);
//        if ($result != false) {
//            $sess_array = array();
//            $result = $result[0];
//            $sess_array = array(
//                'id' => $result->id,
//                'username' => $result->username,
//                'gid' => $result->gid,
//                'su' => $result->su,
//                'title' => $result->title);
//
//            //$this->session->set_userdata('logged_in', $sess_array);
//
//            $this->session->set_userdata('logged_in', md5($sess_array['id']) . md5($sess_array['username']));
//
//
//            
//
//
//
//
//            return TRUE;
//        } else {
//            //$this->form_validation->set_message('check_database', 'Invalid username or password');
//            return array('Success' => false, 'Message' => 'Invalid username or password');
//        }
//    }
}

?>
